package cn.mumzone.pos.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.source.AppRepository;

public class ACT_ShopConfig extends BaseActivity {


    @BindView(R.id.sv_open_schedule)
    SwitchView svOpenSchedule;
    @BindView(R.id.sv_print_off_work_ticket)
    SwitchView svPrintOffWorkTicket;
    @BindView(R.id.sv_guest_display)
    SwitchView svGuestDisplay;

    @BindView(R.id.rg_print_count)
    RadioGroup rgPrintCount;

    @BindView(R.id.tv_shop_phone)
    TextView tvShopPhone;
    @BindView(R.id.iv_shop_phone_checked)
    ImageView ivShopPhoneChecked;

    @BindView(R.id.tv_shop_address)
    TextView tvShopAddress;
    @BindView(R.id.iv_shop_address_checked)
    ImageView ivShopAddressChecked;

    @BindView(R.id.tv_buy_note)
    TextView tvBuyNote;
    @BindView(R.id.iv_buy_note_checked)
    ImageView ivBuyNoteChecked;

    @BindView(R.id.tv_member_info)
    TextView tvMemberInfo;
    @BindView(R.id.iv_member_info_checked)
    ImageView ivMemberInfoChecked;

    @BindView(R.id.et_buy_note)
    EditText etBuyNote;

    AppRepository appRepository;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(cn.mumzone.basic.R.anim.right_in, cn.mumzone.basic.R.anim.right_out);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.RIGHT;
        getWindow().setAttributes(params);
        getWindow().setBackgroundDrawableResource(R.color.bg_color);
    }

    @Override
    protected void initEventAndData() {
        appRepository = new AppRepository();

        svOpenSchedule.setOpened(AppConstants.CONFIG_OPEN_SCHEDULE);
        svPrintOffWorkTicket.setOpened(AppConstants.CONFIG_PRINT_OFF_WORK_TICKET);
        svGuestDisplay.setOpened(AppConstants.CONFIG_GUEST_DISPLAY);

        int priceCount = AppConstants.CONFIG_PRINT_COUNT;
        switch (priceCount) {
            case 1:
                rgPrintCount.check(R.id.rb_print_one);
                break;
            case 2:
                rgPrintCount.check(R.id.rb_print_two);
                break;
            case 3:
                rgPrintCount.check(R.id.rb_print_three);
                break;
            default:
                rgPrintCount.check(R.id.rb_print_none);
                break;
        }

        etBuyNote.setText(appRepository.getBuyNote());
        etBuyNote.setSelection(StringUtils.isEmpty(appRepository.getBuyNote()) ? 0 : appRepository.getBuyNote().length());

        setCheckedView(R.id.tv_shop_phone, AppConstants.CONFIG_SHOW_SHOP_PHONE);
        setCheckedView(R.id.tv_shop_address, AppConstants.CONFIG_SHOW_SHOP_ADDRESS);
        setCheckedView(R.id.tv_buy_note, AppConstants.CONFIG_SHOW_BUY_NOTE);
        setCheckedView(R.id.tv_member_info, AppConstants.CONFIG_SHOW_MEMBER_INFO);

    }


    @OnClick({R.id.btn_save, R.id.tv_shop_phone, R.id.tv_shop_address, R.id.tv_buy_note, R.id.tv_member_info, R.id.iv_close, R.id.iv_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                int printCount;
                switch (rgPrintCount.getCheckedRadioButtonId()) {
                    case R.id.rb_print_one:
                        printCount = 1;
                        break;
                    case R.id.rb_print_two:
                        printCount = 2;
                        break;
                    case R.id.rb_print_three:
                        printCount = 3;
                        break;
                    default:
                        printCount = 0;
                        break;
                }
                String butNote = etBuyNote.getText().toString().trim();
                appRepository.setAppConfig(svOpenSchedule.isOpened(), svPrintOffWorkTicket.isOpened(), svGuestDisplay.isOpened(), printCount,
                        (Boolean) tvShopPhone.getTag(), (Boolean) tvShopAddress.getTag(), (Boolean) tvBuyNote.getTag(),
                        (Boolean) tvMemberInfo.getTag(), butNote);
                PosToast.INSTANCE.makeSuccessToast(mContext, "保存成功~");
                break;
            case R.id.tv_shop_phone:
                setCheckedView(view.getId(), !(Boolean) tvShopPhone.getTag());
                break;
            case R.id.tv_shop_address:
                setCheckedView(view.getId(), !(Boolean) tvShopAddress.getTag());
                break;
            case R.id.tv_buy_note:
                setCheckedView(view.getId(), !(Boolean) tvBuyNote.getTag());
                break;
            case R.id.tv_member_info:
                setCheckedView(view.getId(), !(Boolean) tvMemberInfo.getTag());
                break;
            case R.id.iv_close:
                finish();
                break;
            case R.id.iv_back:
                ACT_Setting.startAc(mContext);
                finish();
                break;

        }
    }


    public void setCheckedView(int viewId, boolean checked) {
        switch (viewId) {
            case R.id.tv_shop_phone:
                tvShopPhone.setTag(checked);
                ivShopPhoneChecked.setVisibility(checked ? View.VISIBLE : View.GONE);
                break;
            case R.id.tv_shop_address:
                tvShopAddress.setTag(checked);
                ivShopAddressChecked.setVisibility(checked ? View.VISIBLE : View.GONE);
                break;
            case R.id.tv_buy_note:
                tvBuyNote.setTag(checked);
                ivBuyNoteChecked.setVisibility(checked ? View.VISIBLE : View.GONE);
                break;
            case R.id.tv_member_info:
                tvMemberInfo.setTag(checked);
                ivMemberInfoChecked.setVisibility(checked ? View.VISIBLE : View.GONE);
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(cn.mumzone.basic.R.anim.right_in, cn.mumzone.basic.R.anim.right_out);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ACT_Setting.startAc(mContext);
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected int getLayout() {
        return R.layout.act_shop_config;
    }


    public static void startAc(Context context) {
        Intent intent = new Intent(context, ACT_ShopConfig.class);
        context.startActivity(intent);
    }
}
